﻿using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 游戏过程管理器
/// </summary>
public class GameManager : SingletonProvider<GameManager>
{
    // 11111 5个条件
    private uint mAllReady = 31;
    private static Vector3 mBirthPlace = mFirstPlatformPos + new Vector3(0, 3, 0);
    private static Vector3 mFirstPlatformPos = Vector3.zero;
    private static Vector3 mGroundPos = new Vector3(0, -2, 0);
    private static Vector3 mSmallPlatformScale = new Vector3(1, 0.5f, 1);
    private static Vector3 mMiddlePlatformScale = new Vector3(1.5f, 0.5f, 1.5f);
    private static Vector3 mBigPlatformScale = new Vector3(2, 0.5f, 2);
    private static float mMinPlatformDistance = 3;
    private static float mMaxPlatformDistance = 6;
    // 位运算作为判断游戏开始条件
    private uint mGameReady = 0;
    // 下一回合
    public bool isNextRound = false;
    public bool isPlatformReady = false;
    public Transform movableObjects;
    public bool isLose { private set; get; } = false;
    private int mScore = -1;
    // Start is called before the first frame update
    public GameManager()
    {
        movableObjects = new GameObject("MovableObjects").transform;
        MonoManager.GetInstance().AddUpdateListener(CheckGameStart);
    }
    /// <summary>
    /// 准备游戏
    /// </summary>
    /// <param name="isRestart">是否是重开</param>
    public void PrepareGame(bool isRestart)
    {
        PlayerManager.GetInstance().CreatePlayer(mBirthPlace, () => mGameReady |= 1 );
        PlatformManager.GetInstance().CreatePlatform(mFirstPlatformPos, mSmallPlatformScale, () => mGameReady |= 1 << 1);
        if (!isRestart)
        {
            EnvironmentManager.GetInstance().CreateGround(mGroundPos, () => mGameReady |= 1 << 2);
            EnvironmentManager.GetInstance().CreateSky(Vector3.zero, () => mGameReady |= 1 << 3);
            UIManager.GetInstance().CreateMainCanvas(() => mGameReady |= 1 << 4);
        }
    }
    /// <summary>
    /// 检测游戏开始
    /// </summary>
    private void CheckGameStart()
    {
        if (mGameReady == mAllReady)
        {
            MonoManager.GetInstance().RemoveUpdateListener(CheckGameStart);
            MonoManager.GetInstance().AddUpdateListener(CheckNextRound);
        }
    }
    /// <summary>
    /// 检测下一回合
    /// </summary>
    private void CheckNextRound()
    {
        if (isNextRound && !isPlatformReady && !isLose)
        {
            isPlatformReady = true;
            EventCenter.GetInstance().EventTrigger("Notify", "Long press the <size=50><color=#B2500D>Space</color></size> to jump");
            GetNextPlatform();
        }
    }
    /// <summary>
    /// 准备下一个平台
    /// </summary>
    private void GetNextPlatform()
    {
        LinkedList<GameObject> platforms = PlatformManager.GetInstance().mPlatforms;
        Vector3 prePosition = platforms.Last.Value.transform.position;
        Vector3 nextPositon;
        float randDistance = Random.Range(mMinPlatformDistance, mMaxPlatformDistance);
        nextPositon = prePosition + new Vector3(0, 0, randDistance);
        float randScale = Random.Range(0, 1.0f);
        Vector3 scale;
        if (randScale < 0.2f)
        {
            scale = mBigPlatformScale;
        }
        else if (randScale < 0.4f)
        {
            scale = mSmallPlatformScale;
        }
        else
        {
            scale = mMiddlePlatformScale;
        }
        movableObjects.DOMove(movableObjects.position + prePosition - nextPositon, 1).onComplete = () =>
        {
            CameraController.GetInstance().CameraMove(prePosition, 1);
        };
        EventCenter.GetInstance().EventTrigger<Vector3>("Set Jump Direction", nextPositon);
        PlatformManager.GetInstance().CreatePlatform(nextPositon, scale, null);
        InputManager.GetInstance().isOpen = true;
    }
    /// <summary>
    /// 失败
    /// </summary>
    public void Lose()
    {
        EventCenter.GetInstance().EventTrigger("Notify", "You lose!\nPress <size=50><color=#B2500D>R</color></size> to restart");
        InputManager.GetInstance().isOpen = true;
        isLose = true;
        MonoManager.GetInstance().RemoveUpdateListener(CheckNextRound);
    }
    /// <summary>
    /// 设置分数
    /// </summary>
    /// <param name="score"></param>
    public void AddScore(int score)
    {
        mScore += score;
        mScore = mScore < 0 ? 0 : mScore;
        EventCenter.GetInstance().EventTrigger("Change Score", mScore);
    }
    /// <summary>
    /// 重置游戏
    /// </summary>
    public void ReStart()
    {
        if (isLose)
        {
            InputManager.GetInstance().isOpen = false;
            PlatformManager.GetInstance().ClearAllPlatform();
            PlayerManager.GetInstance().ClearPlayer();
            // 地板 天空 UI不需要重新生成
            mGameReady = (1 << 2) + (1 << 3) + (1 << 4);
            isNextRound = false;
            isPlatformReady = false;
            movableObjects.position = Vector3.zero;
            mScore = -1;
            EventCenter.GetInstance().EventTrigger("ResetUI");
            isLose = false;
            PrepareGame(true);
            MonoManager.GetInstance().AddUpdateListener(CheckGameStart);
        }
    }
}
